package com.tengju.bff.interfaces.covercharge.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.tengju.bff.interfaces.covercharge.constant.CoverChargeEnum;
import com.tengju.bff.interfaces.covercharge.constant.CoverChargeErrorCode;
import com.tengju.bff.interfaces.covercharge.entity.vo.ServiceSignQuery;
import com.tengju.bff.interfaces.covercharge.entity.vo.SignAuditRefusedTo;
import com.tengju.bff.interfaces.covercharge.entity.vo.TerminateSign;
import com.tengju.bff.interfaces.covercharge.service.IServiceAccountService;
import com.tengju.bff.interfaces.covercharge.service.IServiceSignService;
import com.tengju.bff.interfaces.covercharge.util.CsvExportUtil;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.export.BillServiceSignExportQuery;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ManageUtil;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.export.application.ExportService;
import com.tengju.export.domain.record.ExportDataType;
import com.tengju.export.domain.record.ExportFileType;
import com.tengju.user.application.user.UserDataAuthService;
import com.tengju.user.domain.model.user.Staff;
import com.tengju.user.domain.model.user.StaffId;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author fwy
 */
@Api(value = "serviceSign", tags = "管理签约记录表")
@RestController
@Slf4j
@RequestMapping("/manage/bill/serviceSign")
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class AdminServiceSignController {

    private final IServiceSignService serviceSignService;
    private final IServiceAccountService iServiceAccountService;
    private final UserDataAuthService userDataAuthService;
    private final ExportService exportService;

    @GetMapping
    @ApiOperation(value = "管理签约记录表", notes = "管理签约记录表")
    @RequiresRoles("sso")
    public ApiResponse<IPage<ServiceSignQuery>> query(@RequestParam(defaultValue = "0", name = "pageNo", required = true) Integer pageNo,
                                                      @RequestParam(defaultValue = "10", name = "limit", required = true) Integer limit,
                                                      @RequestParam(defaultValue = "") String signingTime,
                                                      String idCode,
                                                      @RequestParam(defaultValue = "") String mobile,
                                                      @RequestParam(defaultValue = "") String userName,
                                                      @RequestParam(defaultValue = "") String platformCode,
                                                      @RequestParam(defaultValue = "") String signName,
                                                      @RequestParam(defaultValue = "") String idNumberIdNumber,
                                                      @RequestParam(defaultValue = "") String status,
                                                      @RequestParam(defaultValue = "") String subStatus,
                                                      @RequestParam(defaultValue = "") String methodCooperation,
                                                      @RequestParam(defaultValue = "") String appCode,
                                                      @RequestParam(defaultValue = "") Long ssoUserId
    ) {
        Page resultPage = serviceSignService.queryByPage(new Page<>(pageNo, limit), signingTime,
                idCode,
                mobile,
                userName,
                platformCode,
                signName,
                idNumberIdNumber,
                status,
                subStatus,
                methodCooperation);
        log.info("[支付后台][签约记录管理-查询][请求信息：sso用户id:{}]", ssoUserId);
        Staff staff = ManageUtil.getOperateUser();
        resultPage.setRecords(userDataAuthService.dataMasking(resultPage.getRecords(), staff.getStaffId()));
        return ApiResponse.newSuccess(resultPage);
    }

    @PostMapping("/terminateSign")
    @ResponseBody
    @ApiOperation(value = "签约终止", notes = "签约终止")
    public ApiResponse<Object> terminateSign(@RequestBody TerminateSign terminateSign
    ) {
        ApiResponse<Object> result = serviceSignService.terminateSign(terminateSign.getId(), terminateSign.getAdminId());
        return result;
    }

    @GetMapping("/test")
    @ResponseBody
    @ApiOperation(value = "测试通知", notes = "测试通知")
    public ApiResponse<Object> terminateSign() throws Exception {
        iServiceAccountService.getAccount(CoverChargeEnum.ServiceAccountType.MANAGE_TYPE.getCode(), 2595103);
        //userMessageClient.sendDtServiceFeeMessage(2594623, "哈哈java", "哈哈", true, true, true);
        ApiResponse<Object> resultJson = ApiResponse.newSuccess();
//        resultJson.setMsg(result);
        return resultJson;
    }

//    private UserMessageDTO buildSendMessage(){
////        UserMessageDTO userMessageDTO=new UserMessageDTO();
////        userMessageDTO.setTitle("等级变更通知");
////        userMessageDTO.setContent(new StringBuilder()
////                .append("您的好友").append("(").append("dasd").append("将会重新跟您建立关系，否则将永久脱离").toString()
////        );
////        userMessageDTO.setLink("");
////        userMessageDTO.setImageUrl("http://img.idanchuang.com/message/dt_default.png");
////        userMessageDTO.setType(String.valueOf(TypeChannelEnum.DT_NOTICE.getType()));
////        userMessageDTO.setJumpType(MessageActionType.JumpToLink.getValue());
////        Integer pid = 2594623;
////        userMessageDTO.setUserId(Long.valueOf(pid));
////        userMessageDTO.setCode("DT");
//
////        userMessageDTO.setPushWayList(Arrays.asList(PushWayType.INSIDE.getValue()));
////        return userMessageDTO;
//        return null;
//
//    }

    @PostMapping("/approv")
    @ResponseBody
    @ApiOperation(value = "审核通过", notes = "审核通过")
    public ApiResponse<Object> approv(@RequestBody TerminateSign terminateSign
    ) {
        ApiResponse<Object> result = serviceSignService.approv(terminateSign.getId(), terminateSign.getAdminId());
        return result;
    }

    @PostMapping("/auditRefusedTo")
    @ResponseBody
    @ApiOperation(value = "审核拒绝", notes = "审核拒绝")
    public ApiResponse<Object> auditRefusedTo(@RequestBody SignAuditRefusedTo signAuditRefusedTo
    ) {
        ApiResponse<Object> result = serviceSignService.auditRefusedTo(signAuditRefusedTo.getId(), signAuditRefusedTo.getAdminId(), signAuditRefusedTo.getRemark());
        return result;
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出签约记录列表")
    public ApiResponse<Object> export(@RequestParam(defaultValue = "1", name = "pageNo", required = true) Integer pageNo,
                                      @RequestParam(defaultValue = "10000", name = "limit", required = true) Integer limit,
                                      @RequestParam(defaultValue = "") String signingTime,
                                      @RequestParam(defaultValue = "") String idCode,
                                      @RequestParam(defaultValue = "") String mobile,
                                      @RequestParam(defaultValue = "") String userName,
                                      @RequestParam(defaultValue = "") String platformCode,
                                      @RequestParam(defaultValue = "") String signName,
                                      @RequestParam(defaultValue = "") String idNumberIdNumber,
                                      @RequestParam(defaultValue = "") String status,
                                      @RequestParam(defaultValue = "") String subStatus,
                                      @RequestParam(defaultValue = "") String methodCooperation,
                                      @RequestParam(defaultValue = "") String appCode,
                                      @RequestParam(defaultValue = "") Long ssoUserId) {

        StaffId staffId = ServletContextUtil.getServletContext().getStaffId();
        String realName = ServletContextUtil.getServletContext().getRealName();

        BillServiceSignExportQuery exportQuery = new BillServiceSignExportQuery();
        exportQuery.setSigningTime(signingTime);
        exportQuery.setIdCode(idCode);
        exportQuery.setMobile(mobile);
        exportQuery.setUserName(userName);
        exportQuery.setPlatformCode(platformCode);
        exportQuery.setSignName(signName);
        exportQuery.setIdNumberIdNumber(idNumberIdNumber);
        exportQuery.setStatus(status);
        exportQuery.setSubStatus(subStatus);
        exportQuery.setMethodCooperation(methodCooperation);
        exportQuery.setStaffId(staffId);

        exportService.exportData("签约记录", ExportFileType.XLSX,
                "billServiceSignExportDataSourceRepository",
                ExportDataType.TABLE, exportQuery,
                staffId.getValue(), realName);
        return ApiResponse.newSuccess();
    }

}
